package com.googlecode.flealancemarket.tapestry.pages.inbox;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.User;
import com.googlecode.flealancemarket.service.commands.CountUnreadMessages;
import com.googlecode.flealancemarket.service.commands.GetUserByUsername;
import com.googlecode.flealancemarket.tapestry.util.Session;
import javax.swing.JOptionPane;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.ioc.annotations.Inject;

public abstract class AbstractInboxPage {

    @ApplicationState
    private Session session;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.CountUnreadMessages")
    private CountUnreadMessages countUnreadMessages;

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.GetUserByUsername")
    private GetUserByUsername getUserByUsername;

    private User user;
    
    private int unread;

    public int getUnread(){
        return unread;
    }

    public void setUnread(int u){
        unread = u;
    }

    public User getUser(){
        return user;
    }

    public void setUser(User u){
        user = u;
    }

    Object onActivate(){
        
	/*
        if(session.getUserId()==0){
            session.setMessage("You cannot access your inbox unless you're logged in.");
            return Login.class;
        }*/
        getUserByUsername.setUsername(session.getUsername());
        user = getUserByUsername.execute();
        countUnreadMessages.setUser(user);
        unread = countUnreadMessages.execute();
        return null;
    }

    public Session getSession(){
        return session;
    }

    void test(String s){
        JOptionPane.showMessageDialog(null, "Poruka: " + s);
    }
}
